unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Edit6: TEdit;
    Edit7: TEdit;
    Edit8: TEdit;
    Edit9: TEdit;
    Label1: TLabel;
    Edit10: TEdit;
    Edit11: TEdit;
    Edit12: TEdit;
    Edit13: TEdit;
    Label2: TLabel;
    Edit14: TEdit;
    Edit15: TEdit;
    Edit16: TEdit;
    Edit17: TEdit;
    Button3: TButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Button4: TButton;
    Button5: TButton;
    Edit18: TEdit;
    Label9: TLabel;
    Edit19: TEdit;
    Label10: TLabel;
    Edit20: TEdit;
    Edit21: TEdit;
    Edit22: TEdit;
    Edit23: TEdit;
    Label11: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label14: TLabel;
    Button6: TButton;
    Button7: TButton;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

Const

MaxSMS    = 50000;       // maximum of SMILES strings
train='+';
calib='-';
test ='#';
valid='*';
VectorDistributionName='VectorDistribution.txt';

var sms:array[1..MaxSMS] of string;
    n:integer; //the number of strin in totalset.txt
    Ntrn,
    Nclb,
    Ntst,
    Nvld: integer; // number Sms in validation set
    wf:string; // for take #TotalSetLV.txt

procedure loadVectorDistribution;
var f:textfile;
    s:string;
    r:real;
begin

 assignfile(f,VectorDistributionName);
 {$I-}
 reset(f);
 {$I+}
 if IOResult <> 0 then
  begin
   showmessage('File '+VectorDistributionName+' not found'+#13+
   'The work is impossible, program will be halted');
   halt;
  end;  // IOresult <>0
  readln(f,r); form2.edit10.Text:=FloatToStrF(r,FFfixed,7,3);
  readln(f,r); form2.edit11.Text:=FloatToStrF(r,FFfixed,7,3);
  readln(f,r); form2.edit12.Text:=FloatToStrF(r,FFfixed,7,3);
  readln(f,r); form2.edit13.Text:=FloatToStrF(r,FFfixed,7,3);
  readln(f,r); form2.edit23.Text:=FloatToStrF(r,FFfixed,7,3);

//  readln(f,s);form2.Edit10.Text:=s;
//  readln(f,s);form2.Edit11.Text:=s;
//  readln(f,s);form2.Edit12.Text:=s;
//  readln(f,s);form2.Edit13.Text:=s;

  closefile(f);
end; // loadVectorDistribution

procedure minimumgaps(var s:string);
label restart;
var k,kk:integer;
begin
restart:
for k:=1 to length(s)-1 do
if (s[k]=' ') and (s[k+1]=' ') then
   begin
   for kk:=k to length(s) do s[kk]:=s[kk+1];
   goto restart;
   end; // k
end; // minimumgaps

procedure getfile(var s,workfile:string);
begin
workfile:='absent';
if s='#TotalSetLV.txt' then workfile:=s;
end; //getfile(s->WorkFile

procedure DefineWorkFile(var wf:string);
var
    fl:TSearchRec;
    fn,
    f1:integer;
//    wf, // work file i.e. #TotalSetLV.txt
    s:string;
begin
f1:=findfirst('#TotalSet*.txt',0,fl);      // whether txt takes place? F1=0
//showmessage(inttostr(f1));
if f1<>0 then
begin
showmessage('No file.txt is available. Please insert desired txt file.');
end; // there are no txt files

s:=fl.Name;
//form1.combobox1.items.add(s);

//showmessage(s); // shoe the first file txt
getFile(s,wf);
//showmessage(wf);

if wf='#TotalSetLV.txt' then exit;
repeat
 fn:=findNext(fl);
 if fn=0 then
 begin
  s:=fl.Name;                       // name of txt file
//  showmessage(s); // shoe the first file txt
  getFile(s,wf);
  if wf='#TotalSetLV.txt' then exit;
//  showmessage(wf);
 end;
until fn<>0;

end; // DefineWorkFile


procedure load;
label start;
var k,j:integer;
    fname:string;
    f:text;
    txt:string;
    r:integer;
begin

txt:='There is File obtained by Las Vegas algorithm.'+#13+
' Would you like use it?';
defineworkfile(wf);

if wf='#TotalSetLV.txt' then
begin
r:=messageDlg(txt,mtConfirmation,[mbYes,mbNo],0);
if r=mrYes then begin form2.Edit1.Text:=wf; end;
end; //dialog

fname:=form2.Edit1.Text;

 assignfile(f,fname);
 {$I-}
 reset(f);
 {$I+}
 if IOResult <> 0 then
  begin
   showmessage('File '+Fname+' not found'+#13+
   'The work is impossible, program will be halted');
   halt;
  end;  // IOresult <>0
  k:=0;
  repeat k:=k+1;
   readln(f,sms[k]);
   for j:=1 to length(sms[k]) do if ord(sms[k][j])=9 then sms[k][j]:=' ';
   minimumgaps(sms[k]);

  until eof(f);
  n:=k;
//  showmessage('Total number is '+inttostr(n));

form2.Label12.Caption:='N = '+IntToStr(n);

end; // load

procedure inc(var x:integer);
begin
x:=x+1;
end; //inc

function classified(var s:string):boolean;
begin
classified:= s[1] in [ train,calib,test,valid ];
end; // classified

procedure DeleteFirstSymbol(var s:string);
var ss:string;
    k:integer;
begin
ss:='';
for k:=2 to length(s) do ss:=ss + s[k];
s:=ss;
end; // delete first symbol

Procedure distribution;
label next,Restart;
Const
limit=1000;
var k:integer;
    Ptrn, Pclb, Ptst, Pvld : real;
    Trn , Clb , Tst , Vld  : real;
    CheckUP, // if probes more than Limit than exit;
    Ntrn, Nclb, Ntst       : integer;
    Act1, Act2, Act3, Act4 : real;
    d, // dispersion in % of distributions into TRN CLB TST VLD
    rnd:real;
    symbol:char;
begin

d:=strtofloat(form2.Edit23.Text);
if d<0.0005 then
begin
d:=0.005;
form2.Edit23.Text:=floatToStrF(d,FFfixed,7,3);
end; // d<0.005

Ptrn:=StrToFloat(form2.Edit10.Text);
Pclb:=StrToFloat(form2.Edit11.Text);
Ptst:=StrToFloat(form2.Edit12.Text);
Pvld:=StrToFloat(form2.Edit13.Text);
(*
showmessage(
'P(TRN) ='+ FloatToStrF(Ptrn,FFfixed,7,2)+#13+
'P(CLB) ='+ FloatToStrF(Pclb,FFfixed,7,2)+#13+
'P(TST) ='+ FloatToStrF(Ptst,FFfixed,7,2)+#13+
'P(VLD) ='+ FloatToStrF(Pvld,FFfixed,7,2)
);
*)
Trn:=Ptrn;
Clb:=Ptrn+Pclb;
Tst:=Ptrn+Pclb+Ptst;
Pvld:=1.0 - Tst;

form2.Edit13.Text := FloatToStrF(Pvld,FFfixed,7,3);

CheckUp:=0;
Restart:
CheckUp:=CheckUp+1;
if CheckUp>Limit then
begin
showmessage
('The dispersion limit is not available. Maybe you should change it.');
exit;
end; //

Ntrn:=0;
Nclb:=0;
Ntst:=0;
Nvld:=0;

for k:=1 to n do
begin
rnd := random;
if rnd<Trn then begin symbol:=train; inc(Ntrn);goto next; end;
if rnd<Clb then begin symbol:=calib; inc(Nclb);goto next; end;
if rnd<Tst then begin symbol:=test ; inc(Ntst);goto next; end;
                      symbol:=valid; inc(Nvld);
next:

if classified(sms[k]) then deleteFirstSymbol(sms[k]);

sms[k]:=symbol+sms[k]

end; // k

Form2.Edit14.Text:=IntTostr(Ntrn);
Form2.Edit15.Text:=IntTostr(Nclb);
Form2.Edit16.Text:=IntTostr(Ntst);
Form2.Edit17.Text:=IntTostr(Nvld);



Act1:=Ntrn/N;
Act2:=Nclb/N;
Act3:=Ntst/N;
Act4:=Nvld/N;

if abs(act1-Ptrn)>D then goto restart;
if abs(act2-Pclb)>D then goto restart;
if abs(act3-Ptst)>D then goto restart;
if abs(act4-Pvld)>D then goto restart;


//showmessage('Distribution completed');

form2.edit19.Text:=FloatToStrF(Act1,FFfixed,7,3);
form2.edit20.Text:=FloatToStrF(Act2,FFfixed,7,3);
form2.edit21.Text:=FloatToStrF(Act3,FFfixed,7,3);
form2.edit22.Text:=FloatToStrF(Act4,FFfixed,7,3);

end; // distribution

procedure TForm2.Button3Click(Sender: TObject);
begin

randomize;

load;
loadVectorDistribution;

if form2.Label6.Caption[1]<>'+' then
form2.Label6.Caption:='+'+form2.Label6.Caption;

end; // load button program buttonClick3

function Is_it_LasVegas(var s:string):boolean;
var rez:boolean;
    x,y:char;
begin

rez:=False;

x:=s[length(s)-5];
y:=s[length(s)-4];

//Showmessage(x+y);

if (x='L') and (y='V') Then rez:=TRUE else rez:=False;

Is_it_LasVegas := rez;

end; // if #TotalSetLV.txt option 2 should be omitted  September25,2024

procedure TForm2.Button1Click(Sender: TObject);
var s:string;
begin
s:=Form2.Edit1.Text;
//showmessage(s);
if Is_it_LasVegas(s) then
begin
showmessage('The file suggested by Las Vegas algorithm, hence the data distribution is not needed');
exit;
end; // this is Las Vegas file

if n=0 then begin showmessage('There is not data'); exit; end;

distribution;

if form2.Label7.Caption[1]<>'+' then
form2.Label7.Caption:='+'+form2.Label7.Caption;
end;

function how_many_TRN_CLB_TST_VLD(parole:char):integer;
var k:integer;
    rez:integer;
begin
rez:=0;
for k:=1 to n do
if sms[k,1]=parole then inc(rez);
how_many_TRN_CLB_TST_VLD:=rez;
end; // how many TRN CLB VLD

procedure TForm2.Button2Click(Sender: TObject);
var
   trnname, vldname, allname : string;
   t,v,a:textfile;
   k:integer;
begin

if n=0 then begin showmessage('There is not data'); exit; end;

   trnname:= Form2.edit2.text;   // TRN
   vldname:= Form2.edit8.text;   // VLD
   allname:= Form2.Edit18.Text;  // Total data for calculation split similarity
   assignfile(t,trnname); rewrite(t);
   assignfile(v,vldname); rewrite(v);
   assignfile(a,allname); rewrite(a);

   Ntrn:=how_many_TRN_CLB_TST_VLD(train);
   Nclb:=how_many_TRN_CLB_TST_VLD(calib);
   Ntst:=how_many_TRN_CLB_TST_VLD(test );
   Nvld:=how_many_TRN_CLB_TST_VLD(valid);

   form2.edit14.Text:=IntToStr(Ntrn);
   form2.edit15.Text:=IntToStr(Nclb);
   form2.edit16.Text:=IntToStr(Ntst);
   form2.edit17.Text:=IntToStr(Nvld);

   form2.edit19.Text:=FloatToStrF(Ntrn/N,FFfixed,7,3);
   form2.edit20.Text:=FloatToStrF(Nclb/N,FFfixed,7,3);
   form2.edit21.Text:=FloatToStrF(Ntst/N,FFfixed,7,3);
   form2.edit22.Text:=FloatToStrF(Nvld/N,FFfixed,7,3);

   writeln(v,Nvld);

   for k:=1 to n do
   begin
   if (sms[k,1]=train) or
      (sms[k,1]=calib) or
      (sms[k,1]=test ) then writeln(t,sms[k]);
   if (sms[k,1]=valid) then writeln(v,sms[k]);
   writeln(a,sms[k]);
   end; // k

   closefile(t);
   closefile(v);
   closefile(a);
//   showmessage('Files are saved');

if form2.Label8.Caption[1]<>'+' then 
form2.Label8.Caption:='+'+form2.Label8.Caption;
end;

procedure TForm2.Button4Click(Sender: TObject);
var s:string;
    f:textfile;
    a,p,c,v,e:real;
begin
assignfile(f,VectorDistributionName);
rewrite(f);

a:=StrToFloat(form2.Edit10.Text); // active training set percentage
p:=StrToFloat(form2.Edit11.Text); // passive training set percentage
c:=StrToFloat(form2.Edit12.Text); // calibration set percentage
v:=StrToFloat(form2.Edit13.Text); // validation set percentage
e:=StrToFloat(form2.Edit23.Text); // epsilon, accuracy of calculations


writeln(f,a);
writeln(f,p);
writeln(f,c);
writeln(f,v);
writeln(f,e);

closefile(f);
end;



procedure TForm2.Button5Click(Sender: TObject);
begin

loadVectorDistribution;

end;

procedure TForm2.Button6Click(Sender: TObject);
begin
form2.close;
end;

procedure TForm2.Button7Click(Sender: TObject);
begin
defineworkfile(Wf);
end;

end.
